<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Goodscat;
use think\Db;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Coupon extends Home
{
    public $noNeedLogin = [];

    /**
     * 获取所有的优惠券
     */
    public function lists(){
        if($this->request->isGet()){
            $getData= $this->request->get();
            if(empty($getData['page'])){
                $getData['page'] = 1;
            }
            //数据验证
            if(!isset($getData['type']) || empty($getData['type'])){
                $this->result('无效的参数','',204);
            }
            $pageData = $this->request->get('limit');
            if(empty($pageData)){
                $limit = 10;
            }else{
                $limit = $pageData;
            }
            //通用券
            if($getData['type'] == 1){
                $coupon =Db::name('qt_coupon')
                    ->field('id as coupon_id,satisfy,reduce,goods_id,img')
                    ->where(['goods_id'=>0,'status'=>1])
                    ->where('end_time','>',time())
                    ->paginate($limit)->each(function ($item){
                        $token = $this->request->header('token');
                        $userInfo = Db::name('qt_users')->where('login_token', $token)->find();
                        $couRes = Db::name('qt_coupon_list')->where('cou_id',$item['coupon_id'])->find();
                        $item['img'] = get_img_path($item['img']);
                        $item['start_time'] = $couRes['start_time'];
                        $item['end_time'] = $couRes['end_time'];
                        if(empty($userInfo)){
                            $item['is_receive'] = '0';
                        }else{
                            $find = Db::name('qt_coupon_list')->where(['cou_id'=>$item['coupon_id'],'user_id'=>$userInfo['id']])->find();
                            if(empty($find)){
                                $item['is_receive'] = '0';
                            }else{
                                $item['is_receive'] = '1';
                            }
                        }
                        return $item;
                    });
            }elseif($getData['type'] ==2){
                //单品券
                $coupon =Db::name('qt_coupon')
                    ->field('id as coupon_id,satisfy,reduce,goods_id,img')
                    ->where(['status'=>1])
                    ->where('goods_id','<>',0)
                    ->where('end_time','>',time())
                    ->paginate()->each(function ($item){
                        $couRes = Db::name('qt_coupon_list')->where('cou_id',$item['coupon_id'])->find();
                        $item['img'] = get_img_path($item['img']);
                        $item['start_time'] = $couRes['start_time'];
                        $item['end_time'] = $couRes['end_time'];
                        $token = $this->request->header('token');
                        $userInfo = Db::name('qt_users')->where('login_token', $token)->find();
                        if(empty($userInfo)){
                            $item['is_receive'] = '0';
                        }else{
                            $find = Db::name('qt_coupon_list')->where(['cou_id'=>$item['coupon_id'],'user_id'=>$userInfo['id']])->find();
                            if(empty($find)){
                                $item['is_receive'] = '0';
                            }else{
                                $item['is_receive'] = '1';
                            }
                        }
                        return $item;
                    });
            }else{
                $this->result('无效的参数','',204);
            }
            $this->result('查询成功',$coupon,200);
        }
    }


    /**
     * 我的优惠券
     * @return
     */
    public function my_coupon()
    {
        $userInfo = $this->userInfo;
        if($this->request->isGet()) {
            $type = $this->request->get('type');
            if (empty($type)) {
                $this->result('请求参数有误','',204);
            }
        }
        $getData = $this->request->get();
        if(!isset($getData['type'])){
            $this->result('无效的参数','',204);
        }
        $pageData = $this->request->get('limit');
        if(empty($pageData)){
            $limit = 10;
        }else{
            $limit = $pageData;
        }
       //正常使用的
        if($type == 1){
            $where[] = ['qt_coupon_list.end_time','>', time()];
        }elseif($type == 2){
            //已经过期的
            $where[] = ['qt_coupon_list.end_time','<', time()];
        }
        //couponList
        $couponList = Db::view('qt_coupon_list', 'id,cou_id,start_time,end_time,satisfy,reduce')
            ->view('qt_coupon', 'img', 'qt_coupon.id=qt_coupon_list.cou_id', 'left')
            ->where(['qt_coupon.status'=>'1'])
            ->where($where)
            ->where('qt_coupon_list.user_id',$userInfo['id'])
            ->order('qt_coupon_list.id','desc')
            ->paginate($limit);
        //处理优惠券图片
        foreach ($couponList->items() as $key=>$value){
            $couponList->items()[$key]['img'] = get_img_path($value['img']);
            if($type == 2){
                Db::name('qt_coupon_list')->where(['id'=>$value['id']])->update(['is_use'=>3]);
            }
        }
        $this->result('查询成功',$couponList,200);
    }

    /**
     * 领取优惠券
     */
    public function get_coupon(){
        if($this->request->isGet()){
            $getData = $this->request->get();
            $userInfo = $this->userInfo;
            if(empty($getData['id'])){
                $this->result('参数不正确','',204);
            }
            $couponRes = Db::name('qt_coupon')
                ->alias('c')
                ->leftJoin('qt_coupon_list cl','c.id = cl.cou_id')
                ->field('cl.end_time')
                ->where('cl.cou_id',$getData['id'])
                ->find();
            if($couponRes['end_time'] < time()){
                $this->result('优惠券已过期','',204);
            }
            $find = Db::name('qt_coupon_list')->where(['user_id'=>$userInfo['id'],'cou_id'=>$getData['id']])->find();
            if(empty($find)){
                $find = Db::name('qt_coupon_list')->where(['cou_id'=>$getData['id']])->whereNull('user_id')->find();
                if($find){
                    $update = Db::name('qt_coupon_list')->where(['id'=>$find['id']])->update(['user_id'=>$userInfo['id'],'linqu_time'=>time(),'is_use'=>'1']);
                    if($update !== false){
                        $this->result('领取成功','',200);
                    }
                    $this->result('领取失败','',204);
                }
                $this->result('优惠券已经没有了','',204);
            }
            $this->result('已经领取过了','',204);
        }
    }


}
